package com.project.estate.activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.estate.activity.service.ActivityService;
import com.project.estate.commons.bean.Activity;
import com.project.estate.commons.bean.Car;
import com.project.estate.commons.config.JsonResult;
import com.project.estate.commons.config.ResultTool;
import com.project.estate.commons.handler.*;
import com.project.estate.commons.mapper.ActivityMapper;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    //添加
    @Override
    public JsonResult addActivity(Activity activity) {
        boolean save = save(activity);
        if(!save){
            throw new ActivityAddException("添加失败");
        }
        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteActivity(String comId) {
        UpdateWrapper<Activity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);
        updateWrapper.set("status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new ActivityDeleteException("删除失败");
        }
        return ResultTool.success("success");
    }

    //修改
    @Override
    public JsonResult updateActivity(Activity activity, String comId) {
        UpdateWrapper<Activity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);
        updateWrapper.set("address",activity.getAddress());
        updateWrapper.set("community_name", activity.getCommunityName());
        updateWrapper.set("start_time", activity.getStartTime());
        updateWrapper.set("end_time", activity.getEndTime());
        updateWrapper.set("organizer",activity.getOrganizer());
        updateWrapper.set("title",activity.getTitle());

        boolean update = update(updateWrapper);
        if (!update) {
            throw new ActivityUpdateException("修改失败");
        }
        return ResultTool.success("success");
    }

    //查询
    @Override
    public JsonResult findActivity(String comId) {
        QueryWrapper<Activity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("community_id", comId);
        List<Activity> list = list(queryWrapper);
        if (list.isEmpty()) {
            throw new ActivityFindException("未查询到");
        }
        return ResultTool.success(list);
    }

    @Override
    public JsonResult findAllActivity() {
        return null;
    }

    /*@Override
    public JsonResult addActivity(Activity activity) {
        boolean save = save(activity);
        if(!save){
            throw new ActivityAddException("添加失败");
        }
        return ResultTool.success("success");
    }

    //删除
    @Override
    public JsonResult deleteActivity(String comId) {
        UpdateWrapper<Activity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);
        updateWrapper.set("status", 1);
        boolean update = update(updateWrapper);
        if (!update) {
            throw new CarDeleteException("删除失败");
        }
        return ResultTool.success("success");
    }

    //修改
    @Override
    public JsonResult updateActivity(Activity activity, String comId) {
        UpdateWrapper<Activity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("community_id", comId);

        boolean update = update(updateWrapper);
        if (!update) {
            throw new CarUpdateException("修改失败");
        }
        return ResultTool.success("success");

    }

    //查询
    @Override
    public JsonResult findActivity(String comId) {
        return null;
    }

    //全查询
    @Override
    public JsonResult findAllActivity() {
        return null;
    }*/

}
